package com.nit.ehcache;

import com.nit.ehcache.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SpringBootApplication
@EnableCaching
@RestController
@RequestMapping("/test")
@CrossOrigin(origins ="*",maxAge = 3600)
public class EhcacheApplication {

	public static void main(String[] args) {
		SpringApplication.run(EhcacheApplication.class, args);
	}

	private static List<User> list = new ArrayList<>();

	public @PostConstruct
	void init() {
		for (int i = 1000; i < 1010; i++) {
			list.add(new User().setId(i).setDate(new Date()).setName("张" + i).setTimestamp(new Timestamp(5000000L + i * 1000)));
		}
	}


	@GetMapping
	public ResponseEntity<Object> get(HttpServletRequest request) {
		System.out.println(request.getRequestURI());
		System.out.println(request.getParameter("ID"));
		return new ResponseEntity<>(request.getParameter("ID"), HttpStatus.OK);
	}

	@PostMapping
	public ResponseEntity<Object> post(@Validated @RequestBody User user) {
		return new ResponseEntity<>(user, HttpStatus.OK);

	}

	@PutMapping
	public ResponseEntity<Object> put(User user) {
		return new ResponseEntity<>(user, HttpStatus.OK);

	}

	@DeleteMapping
	public ResponseEntity<Object> delete(Integer[] ids) {
		ArrayList<User> users = new ArrayList<>();
		for (int id : ids) {
			users.add(list.get(id));
		}
		return new ResponseEntity<>(users, HttpStatus.OK);

	}

}
